import 'package:flui/flui.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:lepig/model/SelectShopPlatformModel.dart';
import 'package:lepig/util/JumpUtil.dart';


class ShopBoxs extends StatelessWidget {
  ShopBoxs({this.lists});

  List<SelectShopPlatformModel> lists = [];
  int index = 0;
  int coloum = 2;

  List<Widget> buildLists(List<SelectShopPlatformModel> lists, int column) {
    List<List<SelectShopPlatformModel>> newShopCate = [];
    int copies = ((lists.length) / coloum).floor();
    //取余加1
    if ((lists.length) % coloum > 0) {
      copies += 1;
    }
    for (int i = 1; i <= copies; i++) {
      List<SelectShopPlatformModel> list;
      int index = (i - 1) * coloum;
      if ((lists.length - index) < coloum) {
        list = lists.sublist(index, lists.length);
      } else {
        list = lists.sublist(index, index + coloum);
      }
      newShopCate.add(list);
    }
    List<Widget> children = [];
    for (int i = 0; i < newShopCate.length; i++) {
      List<Widget> subChildren = [];
      for (int j = 0; j < newShopCate[i].length; j++) {
        subChildren.add(shopItem(newShopCate[i][j]));
      }
      children.add(new Column(
        children: subChildren,
      ));
    }
    return children;
  }

  BuildContext context;

  @override
  Widget build(BuildContext context) {
    this.context = context;
    // TODO: implement build
    return Container(
      margin: EdgeInsets.only(top: 10.0, bottom: 10.0),
      padding: EdgeInsets.only(top: 10.0, bottom: 15.0),
      height: 165,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10.0),

      ),
      child: ListView(
        scrollDirection: Axis.horizontal,
        children: lists.length == 0 ? _loadItem(): buildLists(lists, 3),
      ),
    );
  }

  shopItem(SelectShopPlatformModel platformModel) {
    return new InkWell(
      child: new Container(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Image.network(
              platformModel.icon,
              width: 50,
              height: 50,
            ),
            new Text(
              platformModel.title,
              style: new TextStyle(fontSize: 13.0),
            )
          ],
        ),
        width: 80,
        height: 70,
        alignment: Alignment.center,
      ),
      onTap: () {
        JumpUtil(context, jumpUrl: platformModel.jumpUrl,
            jumpKey: platformModel.jumpKey,
            jumpName: platformModel.title,
            jumpPositon: platformModel.jumpPositon).go();
      },
    );
  }

  List<Widget> _loadItem() {
    List<Widget> list = [];
    for (int i = 0; i < 6; i++) {
      list.add(new Column(
        children: <Widget>[
          FLSkeleton(
            shape: BoxShape.circle,
            width: 80,
            height: 60,
          ),
          new SizedBox(
            height: 10.0,
          ),
          FLSkeleton(
            shape: BoxShape.circle,
            width: 80,
            height: 60,
          ),
        ],
      ));
    }
    return list;
  }
}


